﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using YLClient.Froms;

using YLClient.Util;
using YLClient.YLServiceClient;
using YLPublicService.TransferModel;
using YLClient.BLL;
using YLClient.RedLetterDays;

namespace YLClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DateTime[] currentDates;
        public MainWindow()
        {
            InitializeComponent();
            RedLetterDayConverter.Initial();
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            NewItem newItem = new NewItem();
            newItem.ShowDialog();
        }

        private IEnumerable<UserControls.Item> buildItemControls(IWorkItem[] items)
        {
            foreach (var item in items)
            {
                UserControls.Item i = new UserControls.Item(item);
                yield return i;
            }
        }

        private void GetItems_Click(object sender, RoutedEventArgs e)
        {

        }


        private void getUserAccount_Click(object sender, RoutedEventArgs e)
        {
            //string info = "";
            //info += "当前余额:" + DataContainer.Account.Money.ToString();
            //MessageBox.Show(info, "帐户余额");
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            var client = ServiceClient.GetServiceClient();
            client.LoginOut();
        }

        private void setItems(IEnumerable<IWorkItem> items)
        {
            lstItems.Items.Clear();
            foreach (var item in items)
            {
                lstItems.Items.Add(new UserControls.Item(item));
            }
            lstItems.Items.Refresh();
        }

        void searcher_OnResultArrive(IEnumerable<IWorkItem> items)
        {
            setItems(items);
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            Froms.Searcher searcher = new Searcher();
            searcher.OnResultArrive += wis => searcher_OnResultArrive(wis);
            searcher.ShowDialog();
        }

        private void Setting_Click(object sender, RoutedEventArgs e)
        {
            Froms.SettingManager sm = new SettingManager();
            sm.ShowDialog();
        }

        private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            var date = (DateTime)e.AddedItems[0];
            var nextDate = date.Add(new TimeSpan(1, 0, 0, 0));
            var client = ServiceClient.GetServiceClient();
            var workitems = client.GetWorkItems(
                new DateTime(date.Year, date.Month, date.Day),
                new DateTime(nextDate.Year, nextDate.Month, nextDate.Day))
                .Select(wi =>
                    new WorkItemEx(wi))
                    ;
            searcher_OnResultArrive(workitems);
        }
    }
}
